package com.mei.demo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.mei.demo.domain.Dictionary;
import com.mei.demo.enums.DictionaryTypeEnum;
import com.mei.demo.response.ResponseUtil;
import com.mei.demo.service.DictionaryService;

/**
 * 数据字典组
 *
 * @author mei.xiaoliang@qq.com
 */
@RequestMapping("/dictionary")
@RestController
public class DictionaryController extends BaseController {

    @Autowired
    DictionaryService dictionaryService;

    @RequestMapping("/index")
    public ModelAndView index() {
        ModelAndView mov = new ModelAndView();
        mov.setViewName("/dictionary/index");
        return mov;
    }

    /**
     * 查询所有字典，前端 ztree 构建字典树
     */
    @RequestMapping(value = "listAll", method = { RequestMethod.GET, RequestMethod.POST })
    public String listAll() {
        System.out.println(DictionaryTypeEnum.DICT);
        List<Dictionary> list = dictionaryService.listAll();
        return ResponseUtil.buildSuccessResponse(list);
    }

    /**
     * 数据字典详情
     */
    @RequestMapping(value = "browse", method = { RequestMethod.GET, RequestMethod.POST })
    public String browse(Integer id) {
        Dictionary dictionary = dictionaryService.browse(id);
        return ResponseUtil.buildSuccessResponse(dictionary);
    }

    /**
     * 新增
     */
    @RequestMapping(value = "add", method = { RequestMethod.GET, RequestMethod.POST })
    public String add(Dictionary dictionary) {
        dictionary.setType(DictionaryTypeEnum.DICT.getCode());
        dictionaryService.add(dictionary);
        return ResponseUtil.buildSuccessResponse();
    }

    /**
     * 修改
     */
    @RequestMapping(value = "update", method = { RequestMethod.GET, RequestMethod.POST })
    public String update(Dictionary dictionary) {
        dictionaryService.update(dictionary);
        return ResponseUtil.buildSuccessResponse();
    }

    /**
     * 删除
     */
    @RequestMapping(value = "delete", method = { RequestMethod.GET, RequestMethod.POST })
    public String delete(Dictionary dictionary) {
        dictionaryService.delete(dictionary);
        return ResponseUtil.buildSuccessResponse();
    }

}